$(".inf").click(function (){
    console.log(($(this)).parents(".pic").children(".productId").text());
    $.ajax({
        type:"get",
        url:"/getProductInf",
        dataType:"json",
        contextType:"application/json;charset=UTF-8",
       data:'productId='+$(this).parents(".pic").children(".productId").text(),
       // data:'productId='+$(this).parents('div').children('#thisProductId').text(),
       // data:'productId='+$(this).children('#thisProductId').text(),
        success:function (result){
                        console.log(result);
             for(var i=0;i<result.length;i++){
                 $("#productId").val(result[i].productId);
                 $("#categoryId").val(result[i].categoryId);
                 $("#description").val(result[i].description);
                 $("#name").val(result[i].name);
                 // console.log( $("#productId").val(result[i].productId));

            }
        }
    })
})

$(".upload").click(function (){
    // 获取要上传的图片
    var image = $(this).parents(".pic").children(".file")[0].files[0];
    // 获取与图片相关的字符串
    var product = $(this).parents(".pic").children(".productId").text();
    var reader = new FileReader();
    var imgURL;
    reader.readAsDataURL(image);
    reader.onload = function (e){
        imgURL = this.result;
    }
    var pic = $(this).parents(".app-card-body").children(".item")
        .children(".row").children(".col-auto").children(".test")
        .children(".item-data").children(".profile-image");
    // 创建FormData对象
    var file = new FormData();
    // 添加图片和相关字符串到表单
    file.append("image", image);
    // formData.append("product", product);

    $.ajax({
        type:"post",
        url:"/upload",
        cache:false,
        async:true,
        data:file,
        dataType:'json',
        processData:false,
        contentType:false,
        headers:{'product':product},
        success:function (result){
            console.log("success");
        },
        error:function (result){
            console.log("fail");
            pic.attr('src',imgURL);
        }
    })
})

$(".deleteProduct").click(function () {
    // 获取与图片相关的字符串
    var product = $(this).parents(".pic").children(".productId").text();
    $.ajax({
        type: "delete",
        url: "/deleteProduct",
        data: 'product=' + product,
        dataType: 'json',
        success: function (result) {
            console.log("success");
            // location.reload(true);

        },
        error: function (result) {
            console.log("fail");
            location.reload(true);
        }
    })
})

function showPopup() {
    document.getElementById("overlay").style.display = "block";
    document.getElementById("popup").style.display = "block";
}

function hidePopup() {
    document.getElementById("overlay").style.display = "none";
    document.getElementById("popup").style.display = "none";
}

function showPopup2() {
    document.getElementById("overlay-name").style.display = "block";
    document.getElementById("popup-name").style.display = "block";
}

function hidePopup2() {
    document.getElementById("overlay-name").style.display = "none";
    document.getElementById("popup-name").style.display = "none";
}

function showPopup3() {
    document.getElementById("overlay-description").style.display = "block";
    document.getElementById("popup-description").style.display = "block";
}

function hidePopup3() {
    document.getElementById("overlay-description").style.display = "none";
    document.getElementById("popup-description").style.display = "none";
}